package dao

import (
	admin "admin_srv/basic/proto"
	"admin_srv/handler/model"
	"fmt"
)

// 添加虚拟商品
func CreateVirtualGoods(in *admin.CreateVirtualGoodsReq) (*model.VirtualGoods, error) {
	goods := &model.VirtualGoods{
		Name:          in.Name,
		Type:          uint(in.Type),
		ImageUrl:      in.ImageUrl,
		Price:         in.Price,
		CurrencyType:  uint(in.Type),
		Description:   in.Description,
		EffectConfig:  in.EffectConfig,
		DurationDays:  uint(in.DurationDays),
		StockQuantity: int(in.StockQuantity),
		IsAvailable:   1,
		SortOrder:     uint(in.SortOrder),
	}
	if err := goods.CreateVirtualGoods(); err != nil {
		return nil, fmt.Errorf("创建虚拟商品失败")
	}
	return goods, nil
}

// 更新商品
func UpdateVirtualGoods(in *admin.UpdateVirtualGoodsReq) (*model.VirtualGoods, error) {

	var goodsVirtual model.VirtualGoods
	if err := goodsVirtual.GetVirtualDetail(&model.VirtualGoods{Id: uint(in.Id)}); err != nil {
		return nil, fmt.Errorf("获取虚拟商品信息失败")
	}
	if goodsVirtual.Id == 0 {
		return nil, fmt.Errorf("该虚拟商品不存在，无法更新")
	}
	goodsVirtual.Name = in.Name
	goodsVirtual.Type = uint(in.Type)
	goodsVirtual.ImageUrl = in.ImageUrl
	goodsVirtual.Price = in.Price
	goodsVirtual.CurrencyType = uint(in.Type)
	goodsVirtual.Description = in.Description
	goodsVirtual.EffectConfig = in.EffectConfig
	goodsVirtual.DurationDays = uint(in.DurationDays)
	goodsVirtual.StockQuantity = int(in.StockQuantity)
	goodsVirtual.SortOrder = uint(in.SortOrder)
	if err := goodsVirtual.UpdateVirtualGoods(&model.VirtualGoods{Id: uint(in.Id)}); err != nil {
		return nil, fmt.Errorf("更新虚拟商品失败")
	}
	return &goodsVirtual, nil
}

// 删除商品
func DeleteVirtualGoods(in *admin.DeleteVirtualGoodsReq) (*model.VirtualGoods, error) {
	var goods model.VirtualGoods
	if err := goods.GetVirtualDetail(&model.VirtualGoods{Id: uint(in.Id)}); err != nil {
		return nil, fmt.Errorf("获取虚拟商品信息失败")
	}
	if goods.Id == 0 {
		return nil, fmt.Errorf("该商品不存在，无法删除")
	}
	if err := goods.DeleteVirtualGoods(&model.VirtualGoods{Id: uint(in.Id)}); err != nil {
		return nil, fmt.Errorf("删除虚拟商品失败")
	}

	return &goods, nil
}

// 获取商品列表
func GetVirtualGoodsList(in *admin.GetVirtualGoodsListReq) ([]*admin.GetVirtualGoodsList, error) {
	var virtualGoodsList model.VirtualGoods
	goods, err := virtualGoodsList.GetVirtualGoods(&model.VirtualGoods{Type: uint(in.Type)}, int(in.Page), int(in.PageSize))
	if err != nil {
		return nil, fmt.Errorf("获取虚拟商品列表失败")
	}
	var virtaulGoods []*admin.GetVirtualGoodsList
	for _, g := range goods {
		virtaulGoods = append(virtaulGoods, &admin.GetVirtualGoodsList{
			Name:          g.Name,
			Type:          int64(g.Type),
			ImageUrl:      g.ImageUrl,
			Price:         g.Price,
			CurrentType:   int64(g.CurrencyType),
			Description:   g.Description,
			DurationDays:  int64(g.DurationDays),
			StockQuantity: int64(g.StockQuantity),
			SortOrder:     int64(g.SortOrder),
			EffectConfig:  g.EffectConfig,
		})
	}
	return virtaulGoods, nil
}

// 获取商品详情
func GetVirtualGoodsDetail(in *admin.GetVirtualGoodsDetailReq) (*model.VirtualGoods, error) {
	var goodsDetail model.VirtualGoods
	if err := goodsDetail.GetVirtualDetail(&model.VirtualGoods{Id: uint(in.Id)}); err != nil {
		return nil, fmt.Errorf("获取虚拟商品详情失败")
	}
	if goodsDetail.Id == 0 {
		return nil, fmt.Errorf("该虚拟商品不存在")
	}
	return &goodsDetail, nil
}

// 启用商品
func EnableVirtualGoods(in *admin.EnableVirtualGoodsReq) (*model.VirtualGoods, error) {
	var enableGoods model.VirtualGoods
	if err := enableGoods.GetVirtualDetail(&model.VirtualGoods{Id: uint(in.Id)}); err != nil {
		return nil, fmt.Errorf("获取虚拟商品信息失败")
	}
	if enableGoods.Id == 0 {
		return nil, fmt.Errorf("该虚拟商品不存在，无法启用")
	}
	if err := enableGoods.EnableGoods(&model.VirtualGoods{Id: uint(in.Id)}); err != nil {
		return nil, fmt.Errorf("更新虚拟商品失败")
	}
	return &enableGoods, nil
}

// 禁用商品
func DisableVirtualGoods(in *admin.DisableVirtualGoodsReq) (*model.VirtualGoods, error) {
	var disableGoods model.VirtualGoods
	if err := disableGoods.GetVirtualDetail(&model.VirtualGoods{Id: uint(in.Id)}); err != nil {
		return nil, fmt.Errorf("获取虚拟商品信息失败")
	}
	if disableGoods.Id == 0 {
		return nil, fmt.Errorf("该虚拟商品不存在，无法禁用")
	}
	if err := disableGoods.DisableGoods(&model.VirtualGoods{Id: uint(in.Id)}); err != nil {
		return nil, fmt.Errorf("禁用虚拟商品失败")
	}
	return &disableGoods, nil

}
